home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / DNS / RR / TSIG.php < prev   
Encoding:
PHP Script  |  2004-03-24  |  7.1 KB  |  236 lines

  1. <?php
  2. /*
  3.  *  License Information:
  4.  *
  5.  *    Net_DNS:  A resolver library for PHP
  6.  *    Copyright (C) 2002 Eric Kilfoil eric@ypass.net
  7.  *
  8.  *    This library is free software; you can redistribute it and/or
  9.  *    modify it under the terms of the GNU Lesser General Public
  10.  *    License as published by the Free Software Foundation; either
  11.  *    version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  *    This library is distributed in the hope that it will be useful,
  14.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *    Lesser General Public License for more details.
  17.  *
  18.  *    You should have received a copy of the GNU Lesser General Public
  19.  *    License along with this library; if not, write to the Free Software
  20.  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  */
  22.  
  23. define("NET_DNS_DEFAULT_ALGORITHM", "hmac-md5.sig-alg.reg.int");
  24. define("NET_DNS_DEFAULT_FUDGE", 300);
  25.  
  26. /* Net_DNS_RR_TSIG definition {{{ */
  27. /**
  28.  * A representation of a resource record of type <b>TSIG</b>
  29.  *
  30.  * @package Net_DNS
  31.  */
  32. class Net_DNS_RR_TSIG extends Net_DNS_RR
  33. {
  34.     /* class variable definitions {{{ */
  35.     var $name;
  36.     var $type;
  37.     var $class;
  38.     var $ttl;
  39.     var $rdlength;
  40.     var $rdata;
  41.     var $time_signed;
  42.     var $fudge;
  43.     var $mac_size;
  44.     var $mac;
  45.     var $original_id;
  46.     var $error;
  47.     var $other_len;
  48.     var $other_data;
  49.     var $key;
  50.  
  51.     /* }}} */
  52.     /* class constructor - RR(&$rro, $data, $offset = "") {{{ */
  53.     function Net_DNS_RR_TSIG(&$rro, $data, $offset = "")
  54.     {
  55.         $this->name = $rro->name;
  56.         $this->type = $rro->type;
  57.         $this->class = $rro->class;
  58.         $this->ttl = $rro->ttl;
  59.         $this->rdlength = $rro->rdlength;
  60.         $this->rdata = $rro->rdata;
  61.  
  62.         if ($offset) {
  63.             if ($this->rdlength > 0) {
  64.                 list($alg, $offset) = Net_DNS_Packet::dn_expand($data, $offset);
  65.                 $this->algorithm = $alg;
  66.  
  67.                 $d = unpack("\@$offset/nth/Ntl/nfudge/nmac_size", $data);
  68.                 $time_high = $d["th"];
  69.                 $time_low = $d["tl"];
  70.                 $this->time_signed = $time_low;
  71.                 $this->fudge = $d["fudge"];
  72.                 $this->mac_size = $d["mac_size"];
  73.                 $offset += 10;
  74.  
  75.                 $this->mac = substr($data, $offset, $this->mac_size);
  76.                 $offset += $this->mac_size;
  77.  
  78.                 $d = unpack("@$offset/noid/nerror/nolen", $data);
  79.                 $this->original_id = $d["oid"];
  80.                 $this->error = $d["error"];
  81.                 $this->other_len = $d["olen"];
  82.                 $offset += 6;
  83.  
  84.                 $odata = substr($data, $offset, $this->other_len);
  85.                 $d = unpack("nodata_high/Nodata_low", $odata);
  86.                 $this->other_data = $d["odata_low"];
  87.             }
  88.         } else {
  89.             if (strlen($data) && preg_match("/^(.*)$/", $data, $regs)) {
  90.                 $this->key = $regs[1];
  91.             }
  92.  
  93.             $this->algorithm   = NET_DNS_DEFAULT_ALGORITHM;
  94.             $this->time_signed = time();
  95.  
  96.             $this->fudge       = NET_DNS_DEFAULT_FUDGE;
  97.             $this->mac_size    = 0;
  98.             $this->mac         = "";
  99.             $this->original_id = 0;
  100.             $this->error       = 0;
  101.             $this->other_len   = 0;
  102.             $this->other_data  = "";
  103.  
  104.             // RFC 2845 Section 2.3
  105.             $this->class = "ANY";
  106.         }
  107.     }
  108.  
  109.     /* }}} */
  110.     /* Net_DNS_RR_TSIG::rdatastr() {{{ */
  111.     function rdatastr()
  112.     {
  113.         $error = $this->error;
  114.         if (! $error) {
  115.             $error = "UNDEFINED";
  116.         }
  117.  
  118.         if (strlen($this->algorithm)) {
  119.             $rdatastr = $this->algorithm . ". " . $this->time_signed . " " .
  120.                 $this->fudge . " ";
  121.             if ($this->mac_size && strlen($this->mac)) {
  122.                 $rdatastr .= " " . $this->mac_size . " " . base64_encode($this->mac);
  123.             } else {
  124.                 $rdatastr .= " 0 ";
  125.             }
  126.             $rdatastr .= " " . $this->original_id . " " . $error;
  127.             if ($this->other_len && strlen($this->other_data)) {
  128.                 $rdatastr .= " " . $this->other_data;
  129.             } else {
  130.                 $rdatastr .= " 0 ";
  131.             }
  132.         } else {
  133.             $rdatastr = "; no data";
  134.         }
  135.  
  136.         return($rdatastr);
  137.     }
  138.  
  139.     /* }}} */
  140.     /* Net_DNS_RR_TSIG::rr_rdata($packet, $offset) {{{ */
  141.     function rr_rdata($packet, $offset)
  142.     {
  143.         $rdata = "";
  144.  
  145.         if (strlen($this->key)) {
  146.             $key = $this->key;
  147.             $key = ereg_replace(" ", "", $key);
  148.             $key = base64_decode($key);
  149.  
  150.             $newpacket = $packet;
  151.             $newoffset = $offset;
  152.             array_pop($newpacket->additional);
  153.             $newpacket->header->arcount--;
  154.             $newpacket->compnames = array();
  155.  
  156.             /*
  157.              * Add the request MAC if present (used to validate responses).
  158.              */
  159.             if (strlen($this->request_mac)) {
  160.                 $sigdata .= pack("H*", $this->request_mac);
  161.             }
  162.             $sigdata .= $newpacket->data();
  163.  
  164.             /*
  165.              * Don't compress the record (key) name.
  166.              */
  167.             $tmppacket = new Net_DNS_Packet;
  168.             $sigdata .= $tmppacket->dn_comp(strtolower($this->name), 0);
  169.  
  170.             $sigdata .= pack("n", Net_DNS::classesbyname(strtoupper($this->class)));
  171.             $sigdata .= pack("N", $this->ttl);
  172.  
  173.             /*
  174.              * Don't compress the algorithm name.
  175.              */
  176.             $tmppacket->compnames = array();
  177.             $sigdata .= $tmppacket->dn_comp(strtolower($this->algorithm), 0);
  178.  
  179.             $sigdata .= pack("nN", 0, $this->time_signed);
  180.             $sigdata .= pack("n", $this->fudge);
  181.             $sigdata .= pack("nn", $this->error, $this->other_len);
  182.  
  183.             if (strlen($this->other_data)) {
  184.                 $sigdata .= pack("nN", 0, $this->other_data);
  185.             }
  186.  
  187.             $this->mac = mhash(MHASH_MD5, $sigdata, $key);
  188.             $this->mac_size = strlen($this->mac);
  189.  
  190.             /*
  191.              * Don't compress the algorithm name.
  192.              */
  193.             unset($tmppacket);
  194.             $tmppacket = new Net_DNS_Packet;
  195.             $rdata .= $tmppacket->dn_comp(strtolower($this->algorithm), 0);
  196.  
  197.             $rdata .= pack("nN", 0, $this->time_signed);
  198.             $rdata .= pack("nn", $this->fudge, $this->mac_size);
  199.             $rdata .= $this->mac;
  200.  
  201.             $rdata .= pack("nnn",$packet->header->id,
  202.                     $this->error,
  203.                     $this->other_len);
  204.  
  205.             if ($this->other_data) {
  206.                 $rdata .= pack("nN", 0, $this->other_data);
  207.             }
  208.         }
  209.         return($rdata);
  210.     }
  211.     /* }}} */
  212.     /* Net_DNS_RR_TSIG::error() {{{ */
  213.     function error()
  214.     {
  215.         if ($this->error != 0) {
  216.             $rcode = Net_DNS::rcodesbyval($error);
  217.         }
  218.         return $rcode;
  219.     }
  220.  
  221.     /* }}} */
  222. }
  223. /* }}} */
  224. /* VIM settings {{{
  225.  * Local variables:
  226.  * tab-width: 4
  227.  * c-basic-offset: 4
  228.  * soft-stop-width: 4
  229.  * c indent on
  230.  * expandtab on
  231.  * End:
  232.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  233.  * vim<600: sw=4 ts=4
  234.  * }}} */
  235. ?>
  236.